<?php
// admin/index.php
require_once '../includes/auth.php';
check_role('admin');

// 获取系统信息
$system_info = [
    '操作系统' => shell_exec('cat /etc/redhat-release'),
    'Apache版本' => apache_get_version(),
    'PHP版本' => phpversion(),
    'MySQL版本' => $pdo->query("SELECT VERSION()")->fetchColumn(),
    '最后启动' => shell_exec('uptime -s')
];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据平台 - 管理后台</title>
    <link rel="stylesheet" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../assets/css/theme.css">
    <style>
        .admin-sidebar {
            background: linear-gradient(180deg, #f8f9fa 0%, #e9ecef 100%);
            border-right: 1px solid #dee2e6;
            min-height: 100vh;
        }

        .nav-link.active {
            background: var(--primary-blue)!important;
            color: white!important;
            border-radius: 8px;
        }

        .info-card {
            background: white;
            border: 1px solid #dee2e6;
            border-radius: 12px;
            transition: all 0.3s;
        }

        .info-card:hover {
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <!-- 侧边栏 -->
        <nav class="col-md-3 col-lg-2 admin-sidebar p-3">
            <h4 class="mb-4 text-primary">管理导航</h4>
            <div class="list-group">
                <a href="#users" class="list-group-item list-group-item-action active">用户管理</a>
                <a href="#roles" class="list-group-item list-group-item-action">角色权限</a>
                <a href="#countries" class="list-group-item list-group-item-action">国家管理</a>
                <a href="#ports" class="list-group-item list-group-item-action">港口管理</a>
                <a href="#monitor" class="list-group-item list-group-item-action">系统监控</a>
                <a href="#modules" class="list-group-item list-group-item-action">模块管理</a>
            </div>
        </nav>

        <!-- 主内容区 -->
        <main class="col-md-9 col-lg-10 p-4">
            <!-- 用户管理 -->
            <section id="users" class="mb-5">
                <h4 class="mb-4 border-bottom pb-2">用户管理</h4>
                <div class="mb-3">
                    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">
                        新增用户
                    </button>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead class="table-light">
                        <tr>
                            <th>用户名</th>
                            <th>角色</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $users = $pdo->query("SELECT * FROM users")->fetchAll();
                        foreach($users as $user): ?>
                            <tr>
                                <td><?=htmlspecialchars($user['username'])?></td>
                                <td><span class="badge bg-primary"><?=$user['role']?></span></td>
                                <td><?=$user['created_at']?></td>
                                <td>
                                    <button class="btn btn-sm btn-outline-primary">编辑</button>
                                    <button class="btn btn-sm btn-outline-danger">删除</button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </section>

            <!-- 系统监控 -->
            <section id="monitor" class="mb-5">
                <h4 class="mb-4 border-bottom pb-2">系统监控</h4>
                <div class="row g-4">
                    <?php foreach($system_info as $title => $value): ?>
                        <div class="col-md-6">
                            <div class="info-card p-3 h-100">
                                <h5 class="text-primary"><?=$title?></h5>
                                <div class="text-muted"><?=htmlspecialchars(trim($value))?></div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </section>

            <!-- 国家管理 -->
            <section id="countries" class="mb-5">
                <h4 class="mb-4 border-bottom pb-2">国家管理</h4>
                <div class="row">
                    <div class="col-md-6">
                        <form class="info-card p-3">
                            <div class="mb-3">
                                <label class="form-label">国家中文名</label>
                                <input type="text" class="form-control" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">国家英文名</label>
                                <input type="text" class="form-control" required>
                            </div>

                            <button type="submit" class="btn btn-primary">添加国家</button>
                        </form>
                    </div>

                    <div class="col-md-6">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <!-- 国家列表 -->
                            </table>
                        </div>
                    </div>
                    <?php
                    $countries = $pdo->query("SELECT * FROM countries ORDER BY name_zh")->fetchAll();
                    ?>
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>中文名称</th>
                            <th>英文名称</th>
                            <th>国家代码</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($countries as $country): ?>
                            <tr>
                                <td><?=htmlspecialchars($country['name_zh'])?></td>
                                <td><?=htmlspecialchars($country['name_en'])?></td>
                                <td><code><?=$country['iso_code']?></code></td>
                                <td>
                                    <button class="btn btn-sm btn-outline-primary">编辑</button>
                                    <button class="btn btn-sm btn-outline-danger">删除</button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                    <section id="countries" class="mb-5">
                        <h4 class="mb-4 border-bottom pb-2">国家管理</h4>
                        <div class="row">
                            <div class="col-md-4">
                                <div class="info-card p-3">
                                    <form method="post" action="country_edit.php">
                                        <div class="mb-3">
                                            <label class="form-label">ISO两位代码</label>
                                            <input type="text" name="iso_code" class="form-control" required maxlength="2">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">ISO三位代码</label>
                                            <input type="text" name="iso3_code" class="form-control" maxlength="3">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">中文名称</label>
                                            <input type="text" name="name_zh" class="form-control" required>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">英文名称</label>
                                            <input type="text" name="name_en" class="form-control" required>
                                        </div>
                                        <button type="submit" class="btn btn-primary w-100">添加国家</button>
                                    </form>
                                </div>
                            </div>

                            <div class="col-md-8">
                                <?php include 'module_mgr.php'; ?>
                                <!-- 动态加载模块管理内容 -->
                            </div>
                        </div>
                    </section>
                </div>
            </section>
        </main>
    </div>
</div>

<!-- 新增用户模态框 -->
<div class="modal fade" id="addUserModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="user_add.php">
                <div class="modal-header">
                    <h5 class="modal-title">新增用户</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">用户名</label>
                        <input type="text" name="username" class="form-control" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">密码</label>
                        <input type="password" name="password" class="form-control" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">角色</label>
                        <select name="role" class="form-select">
                            <option value="admin">管理员</option>
                            <option value="data_admin">数据管理员</option>
                            <option value="user">普通用户</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="../assets/js/bootstrap.bundle.min.js"></script>
<script>
    // 侧边栏导航切换
    document.querySelectorAll('.nav-link').forEach(link => {
        link.addEventListener('click', function(e) {
            document.querySelectorAll('.nav-link').forEach(l => l.classList.remove('active'));
            this.classList.add('active');
        });
    });
</script>
</body>
</html>